/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.command;

import org.eclipse.gef.commands.Command;

import ro.pub.cs.se.moondance.model.Connection;
import ro.pub.cs.se.moondance.editpart.ConnectionEditPart;

public class ChangeConnectionMultiplicityCommand extends Command {

	private Connection conn;
	private String multiplicity, oldMultiplicity;
	private int type;

	public ChangeConnectionMultiplicityCommand(Connection conn, int type) {
		this.conn = conn;
		this.type = type;
	}

	/**
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		if (type == ConnectionEditPart.SOURCE_MULTIPLICITY)
			conn.setSourceMultiplicity(multiplicity);
		else
			conn.setTargetMultiplicity(multiplicity);
	}

	/**
	 * @return whether we can apply changes
	 */
	public boolean canExecute() {

		if (multiplicity != null) {
			return true;
		} else {
			multiplicity = oldMultiplicity;
			return false;
		}
	}

	public void setOldMultiplicity(String string) {
		oldMultiplicity = string;
	}

	public void setMultiplicity(String string) {
		multiplicity = null;
		@SuppressWarnings("unused")
		int firstValue, secondValue, index;
		// first_value..second_value | value | first_value..*
		if ((string != null)
				&& (!Connection.multiplicityValuesList.contains(string))) {
			// not a common multiplicity value
			if ((index = string.indexOf("..")) > 0) { // first_value...second_value
														// | first_value...*
				String tokens[] = new String[2];// string.split("..");
				tokens[0] = string.substring(0, index);
				tokens[1] = string.substring(index + 2);
				if (tokens.length == 2) {
					try {
						firstValue = Integer.parseInt(tokens[0]);
					} catch (NumberFormatException nfe) {
						return;
					}
					if (!tokens[1].equals("*")) {
						try {
							secondValue = Integer.parseInt(tokens[1]);
						} catch (NumberFormatException nfe) {
							return;
						}
					}
				} else
					return;
			} else {// value
				if (!string.equals("*")) {
					try {
						secondValue = Integer.parseInt(string);
					} catch (NumberFormatException nfe) {
						return;
					}
				}
			}
		}
		multiplicity = string;
	}

	/**
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		if (type == ConnectionEditPart.SOURCE_MULTIPLICITY)
			conn.setSourceMultiplicity(oldMultiplicity);
		else
			conn.setTargetMultiplicity(oldMultiplicity);
	}

}